<h1>Using After Hooks</h1>

<p>The goal of an after hook is to intercept an outbound HTTP response and then do <em>something</em> with it <em>before</em> the server issues a response to the end user.</p>

<p>An after hook can be added onto an API endpoint by adding the 'afterHook' property onto an endpoint's settings. Below is an example of an after hook called '_prep_date' being declared for a 'Get' endpoint. This setting (which would be stored on an api.json file) would invoke a protected method named <code>_prep_date()</code> whenever the server is about to issue a response, after having invoked the 'Get' endpoint.</p>

[code]
"Get": {
  "url_segments": "api/get/fish",
  "request_type": "GET",
  "description": "Fetch rows from table",
  "enableParams": true,
  "authorization": "*",
  "afterHook": "_prep_date"
}
[/code]

<h2>The Outbound Response Array</h2>

<p>All the information that gets passed into an after hook comes in the form of an array. We can call this array the 'outbound response array'.</p>

<p>The general structure of an after hook will facilitate taking in an outbound response array, doing something with it, then potentially returning the array. If we represent our outbound response array with the variable <code>$output</code>, then our after hook methods can take the following form:</p>

[code=php]
function _post_insert($output) {
  // do some stuff
  return $output;
}
[/code]

<h2>Diving Deeper</h2>

<p>By using PHP's built-in <code>var_dump()</code> method, in combination with a <code>die</code> statement, it's possible to explore the contents of an outbound response array.</p>

[code=php]
function _post_insert($output) {
  var_dump($output);
  die();
}
[/code]

<p>This will reveal the following three parameters:</p>

<ul>
  <li><strong>token - </strong>If a Trongate security token was passed into the header of the inbound HTTP request, then the value of the security token will be assigned to the 'token' property of the outbound response array. Therefore, to access the token, we could say:</li>
</ul>

[code=php]
$trongate_token = $output["token"];
echo $trongate_token;
die();
[/code]

<ul>
  <li><strong>body</strong> - The body is a string of text that is to be issued by the server. Often, though not always, the body can be JSON decoded into an array of key-value pairs. So, if 'id' with a value of '4' was being issued by the server, we could access the id with:</li>
</ul>

[code=php]
$id = $output["body"]["id"];
echo $id; // outputs 4
die();
[/code]

<ul>
  <li><strong>code</strong> - Finally, our outbound response array will contain a 'code' parameter. The code parameter will be a numeric value representing the HTTP response status code from the server. To display the HTTP response server code, using PHP, we could say:</li>
</ul>

[code=php]
echo $output["code"]; // outputs (something like) 200
die();
[/code]

<div class="alert alert-success">
  Learn about HTTP response status codes <a href="https://trongate.io/docs_m/information/understanding-http-response-status-codes">here</a>.
</div>

<h2>Modifying The Outbound Response Array</h2>

<p>Having access to the outbound HTTP response array means that we have the ability to manipulate the response before it gets served to the end user.</p>

<p>There are lots of situations where this might be useful. One example would be to format dates.</p>

<p><strong>EXAMPLE</strong></p>

<p>Let's imagine that you have an API endpoint that queries a database table and returns information about a record that's stored in the database table.</p>

<p>This could be something like a members table, and it's possible that the response body could take the form of a string of text that could be converted into a JSON object with the following properties:</p>

[code]
{
  "id": 88,
  "username": "Rambo",
  "date_joined": 1629840563
}
[/code]

<p>As you can see, 'date_joined' is an integer that's ten digits in length. When we see this kind of value in PHP, we can be fairly confident that we're being issued with a Unix timestamp. However, before sending the response to the end user, we may wish to use an after hook to intercept the response and format the date so that it's presented in a more user-friendly and readable manner.</p>

<p>Below is an example of an after hook that could modify a response body so that 'date_joined' is nicely formatted.</p>

[code=php]
function _prep_date($output) {
  $date_joined = $output["body"]["date_joined"];
  $output["body"]["date_joined"] = date("l jS F Y", $date_joined);
  return $output;
}
[/code]

<p>The code above would modify the HTTP response body, giving us:</p>

[code]
{
  "id": 88,
  "username": "Rambo",
  "date_joined": "Tuesday 24th August 2021"
}
[/code]
